package com.jlnx.controller.bank;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jlnx.dao.ChainEnterpriseDaoImpl;
import com.jlnx.dao.CoreEnterpriseDaoImpl;
import com.jlnx.dao.EnterpriseDaoImpl;
import com.jlnx.dao.FinanceReqDaoImpl;
import com.jlnx.dao.PayStatusDaoImpl;
import com.jlnx.pojo.ChainEnterprise;
import com.jlnx.pojo.CoreEnterprise;
import com.jlnx.pojo.Enterprise;
import com.jlnx.pojo.FinanceReq;
import com.jlnx.pojo.PayStatus;

/*
 * 同意准入
 */
@WebServlet("/LoanController")
public class LoanController extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获取请求信息
		response.setContentType("application/json;charset=UTF-8;text/html");
		//设置请求编码模式
    	request.setCharacterEncoding("utf-8");
    	
    	int reqId = Integer.parseInt(request.getParameter("reqId"));
				
		 //获取session对象
		 HttpSession hs=request.getSession();
		 //获取登录账号
		 String entNo = ((Enterprise)hs.getAttribute("enterprise")).getEntNo();
		 System.out.println( entNo + ":发起" + reqId + "融资请求的放款");
		FinanceReqDaoImpl dao = new FinanceReqDaoImpl();
		PayStatusDaoImpl paydao = new PayStatusDaoImpl();
		
		FinanceReq financeReq = dao.selectByReqId(reqId);
		Date payDate = new Date();
		// 创建一个Calendar实例
        Calendar calendar = Calendar.getInstance();       
        // 将当前时间设置到Calendar实例中
        calendar.setTime(payDate);       
        // 增加180天
        calendar.add(Calendar.DAY_OF_MONTH, 180);       
        // 获取增加180天后的日期
        Date returnDate = calendar.getTime();       
        // 打印结果
        System.out.println("放款日期：" + payDate + "还款日期：" + returnDate);
		
		//TODO 增加放款记录
		PayStatus payStatus = new PayStatus(reqId, financeReq.getReqName(), financeReq.getEntNo(), financeReq.getName(), payDate, returnDate, 0);
		if(paydao.insertPayStatus(payStatus) < 0) {
			request.setAttribute("isRequestSuccess", false);
			request.getRequestDispatcher("/main/loanSearch.jsp").forward(request, response);
		}
		
		//发起还款，修改还款状态
		if(dao.changeReqStatusById(reqId,4) < 0) {
			request.setAttribute("isRequestSuccess", false);
			request.getRequestDispatcher("/main/loanSearch.jsp").forward(request, response);
		}else {
			request.setAttribute("isRequestSuccess", true);
			request.getRequestDispatcher("/LoanSearchController").forward(request, response);
		}
				
	}

}
